import pymysql
from fastapi import FastAPI,Form,HTTPException

app=FastAPI()
@app.post("/insert")
def insert(name:str=Form(...),password:str=Form(...)):
   global cursor, conn
   try:
       conn = pymysql.Connection(user="root", password="123456Aa", database="2308a")
       cursor = conn.cursor()
       cursor.execute("insert into qq (name,password) values (%s,%s)", (name,password))
       conn.commit()
       return {"msg": "添加成功"}
   except Exception as a:
       print(a)
       raise HTTPException(400, "添加失败")
   finally:
       cursor.close()
       conn.close()

@app.post("/insert1")
def insert(name:str=Form(...),password:str=Form(...)):
   global cursor, conn
   try:
       conn = pymysql.Connection(user="root", password="123456Aa", database="2308a")
       cursor = conn.cursor()
       cursor.execute("select * from qq where name=%s and password=%s", (name,password))
       a=cursor.fetchone()
       if a is None:
           raise HTTPException(400,"失败")
       else:
        return {"msg": "登录成功"}
   except Exception as a:
       print(a)
       raise HTTPException(400, "登录失败")
   finally:
       cursor.close()
       conn.close()